package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.domain.KingBdCangw;
import com.ruoyi.system.mapper.KingBdCangwMapper;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.KingBdCangMapper;
import com.ruoyi.system.domain.KingBdCang;
import com.ruoyi.system.service.IKingBdCangService;

/**
 * 仓库Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-26
 */
@Service
public class KingBdCangServiceImpl implements IKingBdCangService 
{
    @Autowired
    private KingBdCangMapper kingBdCangMapper;
    @Autowired
    private KingBdCangwMapper kingBdCangwMapper;

    /**
     * 查询仓库
     * 
     * @param id 仓库ID
     * @return 仓库
     */
    @Override
    public KingBdCang selectKingBdCangById(Long id)
    {
        return kingBdCangMapper.selectKingBdCangById(id);
    }

    /**
     * 查询仓库列表
     * 
     * @param kingBdCang 仓库
     * @return 仓库
     */
    @Override
    public List<KingBdCang> selectKingBdCangList(KingBdCang kingBdCang)
    {
        return kingBdCangMapper.selectKingBdCangList(kingBdCang);
    }

    /**
     * 新增仓库
     * 
     * @param kingBdCang 仓库
     * @return 结果
     */
    @Override
    public int insertKingBdCang(KingBdCang kingBdCang)
    {
        return kingBdCangMapper.insertKingBdCang(kingBdCang);
    }

    /**
     * 修改仓库
     * 
     * @param kingBdCang 仓库
     * @return 结果
     */
    @Override
    public int updateKingBdCang(KingBdCang kingBdCang)
    {
        return kingBdCangMapper.updateKingBdCang(kingBdCang);
    }

    /**
     * 批量删除仓库
     * 
     * @param ids 需要删除的仓库ID
     * @return 结果
     */
    @Override
    public int deleteKingBdCangByIds(Long[] ids)
    {
        return kingBdCangMapper.deleteKingBdCangByIds(ids);
    }

    /**
     * 删除仓库信息
     * 
     * @param id 仓库ID
     * @return 结果
     */
    @Override
    public int deleteKingBdCangById(Long id)
    {
        return kingBdCangMapper.deleteKingBdCangById(id);
    }

    @Override
    public int getNum(String fId){ return kingBdCangMapper.getNum(fId);}

    @Override
    public String fIdtoName(String fId){ return  kingBdCangMapper.fIdtoName(fId);}

    @Override
    public List<KingBdCang> fCodetoList(KingBdCang kingBdCang){
        return  kingBdCangMapper.fCodetoList(kingBdCang);
    }

    @Override
    public String getCangList(KingBdCang kingBdCang2){
        JSONArray jsonArray = new JSONArray();
        List<KingBdCang> list = kingBdCangMapper.fCodetoList2(kingBdCang2);
        if(list.size()>0){
            for(KingBdCang kingBdCang:list){
                JSONObject jsonObject1 = new JSONObject();
                JSONArray jsonArray1 = new JSONArray();
                List<KingBdCangw> list1 = kingBdCangwMapper.scodeTo(kingBdCang.getfCode());
                if(list1.size()>0){
                    for(KingBdCangw kingBdCangw:list1){
                        JSONObject jsonObject2 = new JSONObject();
                        jsonObject2.put("id",kingBdCangw.getfCode());
                        jsonObject2.put("name",kingBdCangw.getName());
                        jsonObject2.put("pid",kingBdCangw.getsCode());
                        jsonArray1.put(jsonObject2);
                    }
                }
                jsonObject1.put("childs",jsonArray1);
                jsonObject1.put("id",kingBdCang.getfCode());
                jsonObject1.put("name",kingBdCang.getName());
                jsonObject1.put("pid","0");
                jsonArray.put(jsonObject1);
            }
        }
        return jsonArray.toString();
    }

    @Override
    public String getCangList2(KingBdCang kingBdCang2){
        JSONArray jsonArray = new JSONArray();
        List<KingBdCang> list = kingBdCangMapper.fCodetoList(kingBdCang2);
        if(list.size()>0){
            for(KingBdCang kingBdCang:list){
                JSONObject jsonObject1 = new JSONObject();
                JSONArray jsonArray1 = new JSONArray();
                List<KingBdCangw> list1 = kingBdCangwMapper.scodeTo(kingBdCang.getfCode());
                if(list1.size()>0){
                    for(KingBdCangw kingBdCangw:list1){
                        JSONObject jsonObject2 = new JSONObject();
                        jsonObject2.put("id",kingBdCangw.getfCode());
                        jsonObject2.put("name",kingBdCangw.getName());
                        jsonObject2.put("pid",kingBdCangw.getsCode());
                        jsonArray1.put(jsonObject2);
                    }
                }
                jsonObject1.put("childs",jsonArray1);
                jsonObject1.put("id",kingBdCang.getfCode());
                jsonObject1.put("name",kingBdCang.getName());
                jsonObject1.put("pid","0");
                jsonArray.put(jsonObject1);
            }
        }
        return jsonArray.toString();
    }
}
